package web.pio.test;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import web.util.IExcelRowMapper;

public class XlsxMain {

  public static void main( String[] args) throws IOException {
    XlsxMain xlsxMain = new XlsxMain();
    
    xlsxMain.readXlsx();
  }

  private void readXlsx() throws IOException{
    String fileName = "D:\\[工程临时]\\[中学选课与学分鉴定]\\dataimporttest.xlsx";
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook( fileName);
    
    // 循环工作表Sheet
    for(int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++){
      XSSFSheet xssfSheet = xssfWorkbook.getSheetAt( numSheet);
      if(xssfSheet == null){
        continue;
      }
      
      // 循环行Row 
      for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++ ){
        XSSFRow xssfRow = xssfSheet.getRow( rowNum);
        if(xssfRow == null){
          continue;
        }
        
        // 循环列Cell   
        for(int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++){
          XSSFCell xssfCell = xssfRow.getCell( cellNum);
          if(xssfCell == null){
            continue;
          }
          System.out.print("   "+getValue(xssfCell));
        }
        System.out.println();
      }
    }
  }
  
  @SuppressWarnings("static-access")
  private Object getValue(XSSFCell xssfCell){
      switch (xssfCell.getCellType()) {
          case XSSFCell.CELL_TYPE_STRING: // 字符串
              return xssfCell.getStringCellValue();
          case XSSFCell.CELL_TYPE_BOOLEAN: // Boolean
              return xssfCell.getBooleanCellValue();
          case XSSFCell.CELL_TYPE_FORMULA: // 公式  按照算出来的结果取值
              return xssfCell.getNumericCellValue();
//            case HSSFCell.CELL_TYPE_BLANK: // 空值
//                return null;
//            case HSSFCell.CELL_TYPE_ERROR: // 故障
//                return null;
          case XSSFCell.CELL_TYPE_NUMERIC: // 数值型
              Object value = null;
              if (HSSFDateUtil.isCellDateFormatted(xssfCell)) {
                  //  如果是date类型则 ，获取该cell的date值
                  value = xssfCell.getDateCellValue();
              } else { // 纯数字
                  value = xssfCell.getNumericCellValue();
              }
              return value;
      }
      return null;
  }
  
}

